package com.siki.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.siki.dto.enterprise.EnterpriseSaveInfoDTO;
import com.siki.dto.enterprise.SaveTalentPositionDTO;
import com.siki.mapper.EnterpriseMapper;
import com.siki.mapper.TalentPositionMapper;
import com.siki.mapper.UserMapper;
import com.siki.mapper.repo.TalentPositionRepo;
import com.siki.pojo.commonEntity.Enterprise;
import com.siki.pojo.commonEntity.SysUser;
import com.siki.pojo.commonEntity.TalentPosition;
import com.siki.pojo.enumeration.common.AuditStatus;
import com.siki.pojo.enumeration.common.EducationClaim;
import com.siki.pojo.enumeration.common.PositionCategory;
import com.siki.service.EnterpriseService;
import com.siki.service.TalentPositionService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @Author Siki
 * @Date 2020/6/12
 */
@Service
public class EnterpriseServiceImpl implements EnterpriseService {

    private final EnterpriseMapper enterpriseMapper;
    private final UserMapper userMapper;
    private final TalentPositionMapper talentPositionMapper;
    private final TalentPositionRepo talentPositionRepo;
    private final TalentPositionService talentPositionService;

    public EnterpriseServiceImpl(EnterpriseMapper enterpriseMapper, UserMapper userMapper, TalentPositionMapper talentPositionMapper, TalentPositionRepo talentPositionRepo, TalentPositionService talentPositionService) {
        this.enterpriseMapper = enterpriseMapper;
        this.userMapper = userMapper;
        this.talentPositionMapper = talentPositionMapper;
        this.talentPositionRepo = talentPositionRepo;
        this.talentPositionService = talentPositionService;
    }

    @Override
    public void saveInfo(EnterpriseSaveInfoDTO dto, String id) {
        //查询当前用户
        SysUser user = findUserById(id);
        Enterprise enterprise = user.getEnterprise();
        enterprise.setAddress(dto.getAddress());
        enterprise.setAvatarUrl(dto.getAvatarUrl());
        enterprise.setEmail(dto.getEmail());
        enterprise.setIntroduction(dto.getIntroduction());
        //修改信息后需要管理员审核
        user.setAuditStatus(AuditStatus.INFO_UNDER_REVIEW);
        //持久化数据
        enterpriseMapper.updateById(enterprise);
        userMapper.updateById(user);
    }

    @Override
    public List<TalentPosition> getPositionInfo(String id) {
        return Optional.ofNullable(findUserById(id).getEnterprise())
                .map(Enterprise::getTalentPositions).orElse(null);
    }

    @Override
    public void saveTalentPosition(SaveTalentPositionDTO dto, String id) {
        //当前用户
        SysUser user = findUserById(id);
        //获取企业信息
        Enterprise enterprise = user.getEnterprise();
        //新建立岗位实体
        TalentPosition talentPosition;
        if(Objects.nonNull(dto.getId())){
             talentPosition = getTalentPositionInfo(dto.getId());
        }else {
             talentPosition = new TalentPosition();
        }
        talentPosition.setPositionName(dto.getPositionName());
        talentPosition.setPositionCategory(Optional.ofNullable(dto.getPositionCategory()).map(PositionCategory::valueOf).orElse(null));
        talentPosition.setMaximumWage(dto.getMaximumWage());
        talentPosition.setMinimumWage(dto.getMinimumWage());
        talentPosition.setWorkPlace(dto.getWorkPlace());
        talentPosition.setEducationClaim(Optional.ofNullable(dto.getEducationClaim()).map(EducationClaim::valueOf).orElse(null));
        talentPosition.setPositionFeatures(dto.getPositionFeatures());
        talentPosition.setPositionDescription(dto.getPositionDescription());
        //默认未发布
        talentPosition.setReleased(false);
        talentPosition.setEnterprise(enterprise);
        //持久化数据
        List<TalentPosition> positions = enterprise.getTalentPositions();
        //查看该企业有没有已发布的岗位
        if (Objects.nonNull(positions)){
            positions.add(talentPosition);
            talentPositionMapper.insertPositionList(positions);
        }else {
            talentPositionRepo.save(talentPosition);
        }
    }

    @Override
    public SysUser findUserById(String id){
        return Optional.ofNullable(userMapper.selectById(id))
                .orElseThrow(()->new RuntimeException("找不到这个用户:"+id));
    }

    @Override
    public TalentPosition getTalentPositionInfo(Long id) {
        return Optional.ofNullable(talentPositionMapper.selectById(id))
                .orElseThrow(()->new RuntimeException("找不到这个岗位:"+id));
    }

    @Override
    public void deletePosition(Long id) {
        talentPositionMapper.deleteById(id);
    }
}
